package com.ict.web.controller.common;

import com.ict.system.dto.BaseResponse;
import com.ict.system.dto.FileUploadResp;
import com.ict.system.service.FileService;
import com.ict.web.dto.InsertNoticeDto;
import com.ict.web.vo.NoticeVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Api("文件上传")
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传单个文件<br/>
     */
    @ApiOperation(value = "单个文件上传", httpMethod = "POST")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public BaseResponse<FileUploadResp> uploadSingle(
            @ApiParam(value = "file", required = true) @RequestParam("file") MultipartFile file) {
        return BaseResponse.createSuccess(fileService.uploadSingle(file));
    }

    /**
     * 上传文件<br/>
     */
    @ApiOperation(value = "批量文件上传", httpMethod = "POST")
    @RequestMapping(value = "/uploadBatch", method = RequestMethod.POST)
    public BaseResponse<List<FileUploadResp>> uploadBatch(
            @ApiParam(value = "files", required = true) @RequestParam("files") MultipartFile[] files) {
        return BaseResponse.createSuccess(fileService.uploadBatch(files));
    }

    /**
     * @param file
     * @param file2
     * @param dto
     * @return
     */
    @ApiOperation(value = "批量文件上传测试", httpMethod = "POST")
    @RequestMapping(value = "/uploadBatchTest", method = RequestMethod.POST)
    public BaseResponse<String> uploadBatchTest(
            @ApiParam(value = "file", required = false) @RequestParam("file") MultipartFile file, @ApiParam(value =
            "file2", required = false) @RequestParam("file2") MultipartFile file2, InsertNoticeDto dto) {
        StringBuilder sb=new StringBuilder();
        sb.append("返回：");
        if (null != file) {
            FileUploadResp file1F = fileService.uploadSingle(file);
            sb.append(file1F.getFileUrl());
        }
        if (null != file2) {
            FileUploadResp file2F = fileService.uploadSingle(file2);
            sb.append(file2F.getFileUrl());
        }
        return BaseResponse.createSuccess(sb.toString());
    }

    @GetMapping("/download")
    public void downLoad(HttpServletResponse response, @RequestParam("fileName") String fileName) {
        fileService.downLoad(response, fileName);
    }
}
